package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.crab2died.ExcelUtils;

import calendar.Student1;

/**
 * 下载excel和读取excel
 * @author fqcd
 *
 * 2018年4月9日下午4:20:46
 */
@Controller
public class ExcelController {
	
	@RequestMapping(value="c")
	public void justTest(HttpServletResponse response){
        List<Student1> list = new ArrayList<Student1>();
        list.add(new Student1("1010001", "盖伦", "六年级三班",new Date()));
        list.add(new Student1("1010002", "古尔丹","六年级三班",new Date()));
        list.add(new Student1("1010003", "蒙多(被开除了)","六年级三班",new Date()));
        list.add(new Student1("1010004", "萝卜特","六年级三班",new Date()));
        list.add(new Student1("1010005", "奥拉基","六年级三班",new Date()));
        list.add(new Student1("1010006", "得嘞", "六年级三班",new Date()));
        list.add(new Student1("1010007", "瓜娃子", "六年级三班",new Date()));
        list.add(new Student1("1010008", "战三", "六年级三班",new Date()));
        list.add(new Student1("1010009", "李四","六年级三班",new Date()));
        System.out.println("juset tests");
        if(list==null||list.isEmpty()){
        	
        	response.setContentType("text/html");
			PrintWriter out;
			try {
				out = response.getWriter();
				out.println("  <script type='text/javascript'> alert('暂无数据！');window.close();</script>");
				out.println("</HTML>");
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
		
        
        Map<String, String> data = new HashMap<String, String>();

        
        try {
        	response.setHeader("Content-Disposition", "attachment;filename=c.xlsx");
			ExcelUtils.getInstance().exportObjects2Excel(list, Student1.class, true, null, true, response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}
	@RequestMapping(value="b")
	public void test2() throws Exception{
	     String path = "D:\\c.xlsx";
	        List<Student1> students = ExcelUtils.getInstance().readExcel2Objects(path, Student1.class, 0, 0);
	        System.out.println("读取Excel至对象数组(支持类型转换)：");
	        for (Student1 st : students) {
	            System.out.println(st);
	        }
	    }
		
	}


